package cn.hd.userService.controller.function;

import cn.hd.commonService.vo.Result;
import cn.hd.commonService.annotations.LogAnnotation;
import cn.hd.commonService.entity.SysFunction;
import cn.hd.commonService.enums.MethodType;
import cn.hd.userService.service.function.ISysFunctionService;
import cn.hd.userService.vo.function.FunctionInfoVo;
import cn.hd.userService.vo.function.FunctionVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/function")
@Api(tags = "功能操作接口")
public class FunctionController {

    @Autowired
    private ISysFunctionService functionService;

    @PostMapping("/save")
    @ApiOperation(value = "保存或修改功能信息")
    @LogAnnotation(methodName = "保存或修改功能信息",methodDesc = "保存或修改功能信息",methodType = MethodType.POST_TYPE)
    public Result saveOrUpdate(SysFunction sysFunction){
        Assert.notNull(sysFunction,"功能信息有误");
        Assert.hasText(sysFunction.getFuncName(),"功能名称不能为空");
        Assert.notNull(sysFunction.getFuncType(),"功能类型不能为空");
        functionService.saveOrUpdateFunction(sysFunction);
        return Result.success("保存成功");
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除功能信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "functionIds",value = "功能id，以逗号分割",required = true)
    })
    @LogAnnotation(methodName = "删除功能信息",methodDesc = "删除功能信息",methodType = MethodType.DELETE_TYPE)
    public Result deleteBatch(String functionIds){
        Assert.notNull(functionIds,"功能id不能为空");
        functionService.batchDelete(functionIds);
        return Result.success("删除成功");
    }

    @PostMapping("/getFunctionList")
    @ApiOperation(value = "获取功能的信息")
    @LogAnnotation(methodName = "获取功能的信息",methodDesc = "获取功能的信息",methodType = MethodType.FIND_TYPE)
    public Result<FunctionInfoVo> getFunctionList(FunctionVo functionVo){
        return   Result.success("获取成功",functionService.getFunctionList(functionVo));
    }

    @PostMapping("/getAuthorityListByUserId")
    @ApiOperation(value = "根据用户id获取权限信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户id")
    })
    @LogAnnotation(methodName = "根据用户id获取权限信息",methodDesc = "根据用户id获取权限信息",methodType = MethodType.FIND_TYPE)
    public Result getAuthorityListByUserId(Integer userId){
        Assert.notNull(userId,"用户id不能为空");
        return   Result.success("获取成功",functionService.getAuthorityListByUserId(userId));
    }

    @PostMapping("/getFunctionParentList")
    @ApiOperation(value = "获取所有目录或菜单集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "functionName",value = "功能名称"),
            @ApiImplicitParam(name = "funcType",value = "功能类型，默认为1，加载目录，2：加载菜单"),
            @ApiImplicitParam(name = "parentId",value = "功能类型，默认为1，加载目录，2：加载菜单")
    })
    @LogAnnotation(methodName = "获取所有目录或菜单集合",methodDesc = "获取所有目录或菜单集合",methodType = MethodType.FIND_TYPE)
    public Result getListByPage(@RequestParam(defaultValue = "1") String funcType, String functionName,String parentId){
        Assert.hasText(funcType,"功能类型不能为空");
        return   Result.success("获取成功",functionService.getFunctionParentList(funcType,functionName,parentId));
    }
    @PostMapping("/enableFunction")
    @ApiOperation(value = "启用或禁用功能")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "functionId",value = "功能id"),
            @ApiImplicitParam(name = "enable",value = "1：启用，0：禁用，默认为1")
    })
    @LogAnnotation(methodName = "启用或禁用功能",methodDesc = "启用或禁用功能",methodType = MethodType.FIND_TYPE)
    public Result enableFunction(@RequestParam(defaultValue = "1") Integer enable, Integer functionId){
        Assert.notNull(functionId,"功能类型不能为空");
        functionService.enableFunction(functionId,enable);
        return   Result.success("操作成功");
    }

}
